package com.dangz.dans.controller;

import com.alibaba.fastjson.JSONObject;
import com.dangz.dans.entity.CommonResponse;
import com.dangz.dans.response.ApiEnum;
import com.dangz.dans.response.CodeEnum;
import com.dangz.dans.response.TypeEnum;
import com.dangz.dans.utils.EncryptedDataUtils;
import com.dangz.dans.utils.HttpHander;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.io.IOException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("dans/wxInfoUtil")
@Api(tags = "wx_获取相关信息")
public class WxInfoUtilController {
  private final String GET_CODE_URL = "https://api.weixin.qq.com/sns/jscode2session";
  @ApiOperation(value = "获取session_key", notes = "POST方式")
  @ApiImplicitParams({
//            @ApiImplicitParam(paramType = "header", value = "Authorization", name = HttpHeaders.AUTHORIZATION, dataType = "String")
      @ApiImplicitParam(paramType = "query", name = "code", value = "code", dataType = "String", required = true)
  })
  @GetMapping("getCodeData")
  public CommonResponse decode(String code) {
    StringBuilder sb = new StringBuilder(GET_CODE_URL);
    sb.append("?appid=").append("wx0a284ab39e1028a9");
    sb.append("&secret=").append("65594ac975b7f8b3c7f5d7f2354c13c6");
    sb.append("&js_code=").append(code);
    sb.append("&grant_type=authorization_code");
    CommonResponse commonResponse = null;
    try {
      String str = HttpHander.httpGet(sb.toString());
      JSONObject obj = JSONObject.parseObject(str);
      String status = obj.getString("status");
      if (StringUtils.isNotEmpty(status) && "200".equals(status)) {
        commonResponse = new CommonResponse(TypeEnum.getFullRespCode(TypeEnum.Success,
            ApiEnum.XTGL, CodeEnum.SUCCESS_SERVICE_LOCAL), CodeEnum.SUCCESS_SERVICE_LOCAL.getInfo(), str);
      } else {
        commonResponse = new CommonResponse(TypeEnum.getFullRespCode(TypeEnum.Success,
            ApiEnum.XTGL, CodeEnum.SUCCESS_NO_DATA_LOCAL), CodeEnum.SUCCESS_NO_DATA_LOCAL.getInfo());
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
    return commonResponse;
  }
}
